package com.lanou.tipsy.fragment.singer.classify;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.singer.TopAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.singer.singerList.SingerRank;
import com.lanou.tipsy.entity.singer.singerList.SingerRankResult;
import com.lanou.tipsy.fragment.singer.detail.SingersInfoFragment;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/10/21.
 * 歌手-Top100
 */
public class TopFragment extends BaseFragment implements
        AdapterView.OnItemClickListener {
    public static final String TAG = "TopFragment";
    private Context context;
    private TopAdapter adapter;
    private ArrayList<SingerRank> ranks;
    private GridView lv_top;
    private StringRequest request;
    private ResponseErrorListener errorListener;
    private VolleySingleton volleySingleton;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_singer_top, null);
        initView(view);
        analysis();
        return view;
    }

    private void initView(View view) {
        lv_top = (GridView) view.findViewById(R.id.lv_classify);

        errorListener = new ResponseErrorListener();
        volleySingleton = VolleySingleton.getInstance();
        lv_top.setOnItemClickListener(this);
    }

    private void analysis() {
        request = new StringRequest(Constants.SINGERS_TOP, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                SingerRankResult result = JSON.parseObject(response, SingerRankResult.class);
                ranks = result.getData();
                adapter = new TopAdapter(ranks, context);
                lv_top.setAdapter(adapter);
            }
        }, errorListener);
        volleySingleton.addQueue(request, TAG);
    }

    @Override
    public void onStop() {
        super.onStop();
        volleySingleton.removeRequest(TAG);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SingerRank rank = (SingerRank) parent.getItemAtPosition(position);
        FragmentManager manager = getActivity().getSupportFragmentManager();
        SingersInfoFragment fragment = new SingersInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("singers", rank);
        fragment.setArguments(bundle);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.addToBackStack(null);
        transaction.add(R.id.fl_main, fragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.commit();
        Log.i("test", TAG + "->跳转到下一个页面");
    }
}
